package cn.diancan.service.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.diancan.dao.MyBaseDao;
import cn.diancan.exception.OutMsgException;
import cn.diancan.model.Goods;
import cn.diancan.model.Product;
import cn.diancan.model.PurchaseSaleOrderBuyCar;
import cn.diancan.model.types.CommonType;
import cn.diancan.service.GoodsService;
import cn.diancan.service.ProductService;
import cn.diancan.service.PurchaseSaleOrderBuyCarService;
import cn.diancan.util.DateUtil;
import cn.diancan.vo.model.ProductVo;
import cn.diancan.vo.other.Pager;
import cn.diancan.vo.other.ResultVoUtil;

@Component("purchaseSaleOrderBuyCarService")
public class PurchaseSaleOrderBuyCarServiceImpl implements PurchaseSaleOrderBuyCarService {

    @Resource(name="MyBaseDao")
    private MyBaseDao<PurchaseSaleOrderBuyCar> purchaseSaleOrderBuyCarDao;
    
    @Resource(name="productService")
    private ProductService productService;
    
    @Resource(name="goodsService")
    private GoodsService goodsService;
    

     
    public boolean saveAddAndModifyProduct(Long sysManagerId, Long productId, Integer quantity,boolean isAdd)throws OutMsgException,Exception {
	if(null == sysManagerId || null ==productId || null == quantity){
	    throw new OutMsgException("参数错误");
	}
	if(quantity<0){
	    throw new OutMsgException("参数错误");
	}
	
	PurchaseSaleOrderBuyCar purchaseSaleOrderBuyCar=purchaseSaleOrderBuyCarDao.get("FROM PurchaseSaleOrderBuyCar WHERE sysManagerId=? AND productId=? AND isDelete=?", new Object[]{sysManagerId,productId,CommonType.Delete.NO});
	
	if(null!=purchaseSaleOrderBuyCar){ //数量增加  
	    if(isAdd){
		purchaseSaleOrderBuyCar.setQuantity(purchaseSaleOrderBuyCar.getQuantity()+quantity);
	    }else{
		purchaseSaleOrderBuyCar.setQuantity(quantity);//修改
	    }
	    purchaseSaleOrderBuyCarDao.update(purchaseSaleOrderBuyCar);
	    return true;
	}
	 
	Product product  =productService.findByProductId(productId);
	if(null==product){
	    return false;
	}
	Goods goods = goodsService.findByGoodsId(product.getGoodsId());
	if(null==goods){
	    return false;
	} 
	
	purchaseSaleOrderBuyCar =new PurchaseSaleOrderBuyCar();
	purchaseSaleOrderBuyCar.setCreateTime(DateUtil.currentDateTime());
	purchaseSaleOrderBuyCar.setGoodsId(product.getGoodsId());
	purchaseSaleOrderBuyCar.setProductId(productId);
	purchaseSaleOrderBuyCar.setIsDelete(CommonType.Delete.NO);
	purchaseSaleOrderBuyCar.setQuantity(quantity);
	purchaseSaleOrderBuyCar.setRetailPrice(goods.getRetailPrice());
	purchaseSaleOrderBuyCar.setSysManagerId(sysManagerId);
	purchaseSaleOrderBuyCarDao.save(purchaseSaleOrderBuyCar);
	
	return true;
    }
    
    @Override
    public boolean saveAddProduct(Long sysManagerId, Long productId, Integer quantity)   throws OutMsgException, Exception { 
	return saveAddAndModifyProduct(sysManagerId, productId, quantity, true);
    }
    
    @Override
    public boolean updateModifyProduct(Long sysManagerId, Long productId,   Integer quantity) throws OutMsgException, Exception { 
	return saveAddAndModifyProduct(sysManagerId, productId, quantity, false);
    }
    

    public List<ProductVo> findProductVoBySysManagerId(Long sysManagerId,StringBuffer sql,List<Object> param) throws OutMsgException, Exception {
	String hql_s="SELECT p1.goodsId,g2.goodsNumber,g2.goodsName,p1.id AS productId,p1.barCode,g2.tagPrice,g2.retailPrice,p1.stockCount,p1.colorSpecificationValueId,p1.sizeSpecificationValueId,coSp3.value AS colorSpecificationValue,siSp4.value AS sizeSpecificationValue,p0.quantity";
	
	String hql=" FROM PurchaseSaleOrderBuyCar AS p0 LEFT JOIN Product AS p1 ON p0.productId=p1.id LEFT JOIN Goods AS g2 ON p1.goodsId=g2.id LEFT JOIN " +
		" SpecificationValue AS coSp3 ON p1.colorSpecificationValueId= coSp3.id LEFT JOIN " +
		" SpecificationValue AS siSp4 ON p1.sizeSpecificationValueId= siSp4.id " +
		" WHERE p0.isDelete=? AND p1.isDelete=? AND p0.quantity >?";
	
	
	param.add(CommonType.Delete.NO);
	param.add(CommonType.Delete.NO);
	param.add(0) ; //大于 0 
	
	//hql+=" Order by id desc"; 
	
	List<Object[]> objs = purchaseSaleOrderBuyCarDao.findSql(hql_s+hql, param,1,10000);
	List<ProductVo> products = ResultVoUtil.object2Model(ProductVo.class, objs, hql_s+hql);
	sql.append("SELECT count(p0.id)"+hql);
	
	return products;
    }


    @Override
    public List<ProductVo> findProductVoBySysManagerId(Long sysManagerId) throws OutMsgException, Exception { 
	return findProductVoBySysManagerId(sysManagerId, new StringBuffer(), new ArrayList<Object>());
    }
    
    
    
    @Override
    public Pager findByPager(Long sysManagerId, Pager pager)throws OutMsgException, Exception  {
	List<Object> param = new ArrayList<Object>();
	StringBuffer sql =new StringBuffer();
	List<ProductVo> productList= findProductVoBySysManagerId(sysManagerId, sql, param);
	BigInteger count = (BigInteger)purchaseSaleOrderBuyCarDao.countSQL3(sql.toString(), param);
	pager.setList(productList);
	pager.setTotalCount(count.intValue());
	return pager;
    }
    

    @Override
    public boolean deleteAllBySysManagerId(Long sysManagerId) throws OutMsgException, Exception { 
	int resulst= purchaseSaleOrderBuyCarDao.executeSQLHql("Update PurchaseSaleOrderBuyCar Set isDelete=? WHERE sysManagerId=? AND isDelete=?", 
		new Object[]{CommonType.Delete.YES,sysManagerId,CommonType.Delete.NO});
	
	return resulst>=1?true :false;
    }
    
    

 
    
    
    
}
